import { resources, SpriteAtlas, SpriteFrame, TextAsset, Vec2 } from "cc";
/**
 * @class Ab_GameAssets_bA
 * @description 游戏资源管理类，用于加载和获取游戏中的本地化资源。
 */
export default class Ab_GameAssets_bA {
	private static Ab_lang_smap_bA: Map<string, SpriteFrame> = new Map();
	private static Ab_coun_smap_bA: Map<string, SpriteFrame> = new Map();
	private static Ab_lang_tmap_bA: Map<string, string> = new Map();
	private static Ab_nickname_bA: Array<string>;

	public static get Ab_Nickname_bA(){
		return Ab_GameAssets_bA.Ab_nickname_bA;
	}

    /**
     * @method Ab_init_bA
     * @param {string} country - 国家代码
     * @param {string} language - 语言代码
     * @description 初始化游戏资源，加载指定国家和语言的资源包。
     */
	public static async Ab_init_bA(country: string, language: string){
		let [bundle_coun, bundle_lang] = await Promise.all(['C'+country, 'l'+language].map(bundle_name=>abi.Ab_cc_bA.Ab_loadBundle_bA(bundle_name)));

		await (<const>[
			[Ab_GameAssets_bA.Ab_coun_smap_bA, bundle_coun],
			[Ab_GameAssets_bA.Ab_lang_smap_bA, bundle_lang]
		]).Ab_forWait_bA(async ([smap, bundle])=>await abi.Ab_cc_bA.Ab_loadResAsync_bA('texture', SpriteAtlas, bundle).then(atlas=>atlas.getSpriteFrames().forEach(sframe=>smap.set(sframe.name, sframe))));

		await abi.Ab_cc_bA.Ab_loadResAsync_bA('text', TextAsset, bundle_lang).then(file=>{
			let obj = abi.Ab_parseJson_bA(file.text.Ab_decodeAtoB_bA);
			Reflect.ownKeys(obj).forEach(key=>Ab_GameAssets_bA.Ab_lang_tmap_bA.set(key as string, obj[key]));
		});

		await abi.Ab_cc_bA.Ab_loadResAsync_bA('nickname', TextAsset, bundle_coun).then(file=> Ab_GameAssets_bA.Ab_nickname_bA = abi.Ab_parseJson_bA(file.text.Ab_decodeAtoB_bA) );
	}

	/**
	 * 获取国家图标
	 * @param name
	 * @description 获取国家图标\
	 * 0: "icon-symbol"\
	 * 1: "icon-symbol-lock"\
	 * 2: "icon-symbol-light-border"\
	 * 3: "icon-symbol-light-blinblin"
	 */
	public static Ab_getCounTexture_bA(name: string|number){
		if(typeof name == 'number') name = Ab_CounTextureName_bA[name];
		if(!Ab_CounTextureName_bA.includes(name)) name = Ab_CounTextureName_bA[0];

		return Ab_GameAssets_bA.Ab_coun_smap_bA.get(name);
	}

	/**
	 * 获取语言本地化贴图
	 * @param name
	 * @description 获取语言本地化贴图\
	 * 0: "texture1"\
	 * 1: "texture2"\
	 * 2: "texture3"\
	 * 3: "texture4"\
	 * 4: "texture5"\
	 * 5: "texture6"\
	 * 6: "texture7"\
	 * 7: "texture8"
	 */
	public static Ab_getLangTexture_bA(name: string|number){
		if(typeof name == 'number') name = Ab_LangTextureName_bA[name];
		if(!Ab_LangTextureName_bA.includes(name)) throw new Error('Language texture not found');

		return Ab_GameAssets_bA.Ab_lang_smap_bA.get(name);
	}

	/**
	 * 获取语言本地化文本
	 * @param name
	 * @description 获取语言本地化文本\
	 * 0: "word1"\
	 * 1: "word2"\
	 * ...
	 * 67: "word68"
	 */
	public static Ab_getLangText_bA(name: string|number, ...args: any[]){
		if(typeof name == 'number') name = Ab_LangTextName_bA[name];
		if(!Ab_LangTextName_bA.includes(name)) throw new Error('Language text not found');

		let str = Ab_GameAssets_bA.Ab_lang_tmap_bA.get(name).replace(/\\n/g, '\n');
		if(str && args.length > 0) args.forEach(($, i)=>str = str.replace(`{${i+1}}`, String($)));

		return str;
	}

	/** 随机获取一个用户昵称 */
	public static Ab_getNickname_bA(){
		return abi.Ab_array_bA.Ab_random_bA(Ab_GameAssets_bA.Ab_nickname_bA);
	}

	public static async Ab_loadRoundLayout_bA(file_name: string){
		let round_layout: Array<{tier: number, element: Array<{id: number, coor: Array<{x: number, y: number}>}>}>;
		round_layout = await abi.Ab_cc_bA.Ab_loadResAsync_bA('config/'+file_name, TextAsset, resources)
		.then(file=>abi.Ab_parseJson_bA(file.text.Ab_decodeAtoB_bA));

		return round_layout
		.filter(el=>el.element.reduce((acc, cur)=>acc+cur.coor.length, 0) > 0)
		.sort((a, b)=>a.tier-b.tier)
		.map((el, i)=>{
			let coor = el.element.reduce((list, cur)=>{
				list.push(...cur.coor.map(e=>new Vec2(e.x, e.y+0.5)));// 纠正由淇的失误，布局统一调高半块砖
				return list;
			}, [] as Vec2[]);

			return {tier: i+1, coor};
		});
	}

	/**
	 * 加载关卡数据
	 * @param round 描述当前关卡的自然数, 从1开始
	 */
	public static async Ab_loadRoundData_bA(round: number, first_symbol: string){
		let round_layout = await Ab_GameAssets_bA.Ab_loadRoundLayout_bA('level_'+round);
		let round_tile: { round: number; tiers: { tiers: number[]; tiles: { id: number; count: number; }[]; }[]; } = await abi.Ab_cc_bA.Ab_loadResAsync_bA('config/round-tile', TextAsset, resources)
		.then(file=>abi.Ab_parseJson_bA(file.text.Ab_decodeAtoB_bA).find(e=>(e.round+1)==round));

		let id_list = [first_symbol, ...abi.Ab_array_bA.Ab_create_bA(19, i=>(i+1).toString().padStart(2, '0')).sort(()=>Math.random()-0.5)];

		let temp: {tier: number, element: Map<string, Vec2[]>}[] = [];

		round_tile.tiers.forEach(conf=>{
			let bucket: [string, number][] = conf.tiles.map(({id, count})=>[id_list[id-1], count]);

			conf.tiers.forEach(tier=>{
				let item: typeof temp[number] = {tier, element: new Map()};
				temp.push(item);

				round_layout.find(el=>el.tier==tier).coor.forEach(c=>{
					let search = abi.Ab_array_bA.Ab_random_bA(bucket);
					while(search[1] <= 0){
						abi.Ab_array_bA.Ab_remove_bA(bucket, search);
						search = abi.Ab_array_bA.Ab_random_bA(bucket);
					}
					search[1]--;
					if(!item.element.has(search[0])) item.element.set(search[0], []);
					item.element.get(search[0]).push(c);
				});
			});
		});

		return temp.map(({tier, element})=>{
			return {tier, element: Array.from(element).map(([id, coor])=>{
				return {id, coor};
			})};
		}).filter(el=>el.element.reduce((acc, cur)=>acc+cur.coor.length, 0) > 0)
		.sort((a, b)=>a.tier-b.tier)
		.map((el, i)=>{
			el.tier = i+1;
			return el;
		});
	}
}

//0: "icon-symbol"
//1: "icon-symbol-lock"
//2: "icon-symbol-light-border"
//3: "icon-symbol-light-blinblin"
const Ab_CounTextureName_bA = [ "aWNvbi1zeW1ib2w=", "aWNvbi1zeW1ib2wtbG9jaw==", "aWNvbi1zeW1ib2wtbGlnaHQtYm9yZGVy", "aWNvbi1zeW1ib2wtbGlnaHQtYmxpbmJsaW4=" ].map(e=>e.Ab_decodeAtoB_bA);

//0: "texture1"
//1: "texture2"
//2: "texture3"
//3: "texture4"
//4: "texture5"
//5: "texture6"
//6: "texture7"
//7: "texture8"
const Ab_LangTextureName_bA = [ "dGV4dHVyZTE=", "dGV4dHVyZTI=", "dGV4dHVyZTM=", "dGV4dHVyZTQ=", "dGV4dHVyZTU=", "dGV4dHVyZTY=", "dGV4dHVyZTc=", "dGV4dHVyZTg=" ].map(e=>e.Ab_decodeAtoB_bA);

//0: "word1"
//1: "word2"
//...
//67: "word68"
const Ab_LangTextName_bA = [ "d29yZDE=", "d29yZDI=", "d29yZDM=", "d29yZDQ=", "d29yZDU=", "d29yZDY=", "d29yZDc=", "d29yZDg=", "d29yZDk=", "d29yZDEw", "d29yZDEx", "d29yZDEy", "d29yZDEz", "d29yZDE0", "d29yZDE1", "d29yZDE2", "d29yZDE3", "d29yZDE4", "d29yZDE5", "d29yZDIw", "d29yZDIx", "d29yZDIy", "d29yZDIz", "d29yZDI0", "d29yZDI1", "d29yZDI2", "d29yZDI3", "d29yZDI4", "d29yZDI5", "d29yZDMw", "d29yZDMx", "d29yZDMy", "d29yZDMz", "d29yZDM0", "d29yZDM1", "d29yZDM2", "d29yZDM3", "d29yZDM4", "d29yZDM5", "d29yZDQw", "d29yZDQx", "d29yZDQy", "d29yZDQz", "d29yZDQ0", "d29yZDQ1", "d29yZDQ2", "d29yZDQ3", "d29yZDQ4", "d29yZDQ5", "d29yZDUw", "d29yZDUx", "d29yZDUy", "d29yZDUz", "d29yZDU0", "d29yZDU1", "d29yZDU2", "d29yZDU3", "d29yZDU4", "d29yZDU5", "d29yZDYw", "d29yZDYx", "d29yZDYy", "d29yZDYz", "d29yZDY0", "d29yZDY1", "d29yZDY2", "d29yZDY3", "d29yZDY4" ].map(el=>el.Ab_decodeAtoB_bA);
